import {
  USER_GROUP_ADMIN,
  USER_GROUP_STUDENT_ASSIST,
  USER_GROUP_TEACHER_ASSIST,
  USER_GROUP_TEACHER_TRAINER,
  USER_GROUP_TECH_SUPPORT,
  USER_GROUP_FINANCE,
  USER_GROUP_TEACHER,
  USER_GROUP_STUDENT,
} from '../common/groups';

export const JIUQU_ROLE_ADMIN = 1;
export const JIUQU_ROLE_STUDENT_ASSIST = 2;
export const JIUQU_ROLE_TEACHER_TRAINER = 3;
export const JIUQU_ROLE_TECH_SUPPORT = 4;
export const JIUQU_ROLE_FINANCE = 5;
export const JIUQU_ROLE_TEACHER_ASSIST = 6;

/**
 * 获取admin系统的角色映射
 * @param {Number} roleId 用户角色编号
 */
function toLegacyRole(roleId) {
  const ROLE_SUPER = 10;
  const ROLE_ADMIN = 20;
  const ROLE_COURSE_ADMIN = 30;
  const ROLE_TRAINER_LEADER = 100;
  const ROLE_TRAINER = 110;
  const ROLE_HIRING_LEADER = 200;
  const ROLE_HIRING = 210;
  const ROLE_STUDENT_SERVICE_LEADER = 300;
  const ROLE_STUDENT_SERVICE = 310;
  const ROLE_TEACHER_SERVICE_LEADER = 400;
  const ROLE_TEACHER_SERVICE = 410;
  const ROLE_IT_SUPPORT = 500;
  const ROLE_FINANCE = 600;
  const ROLE_MARKETING = 700;
  const ROLE_OTHER = 9999;

  const mapping = {
    [ROLE_SUPER]: JIUQU_ROLE_ADMIN,
    [ROLE_ADMIN]: JIUQU_ROLE_ADMIN,
    [ROLE_COURSE_ADMIN]: JIUQU_ROLE_TEACHER_ASSIST,
    [ROLE_HIRING_LEADER]: JIUQU_ROLE_TEACHER_ASSIST,
    [ROLE_TRAINER]: JIUQU_ROLE_TEACHER_TRAINER,
    [ROLE_TRAINER_LEADER]: JIUQU_ROLE_TEACHER_TRAINER,
    [ROLE_HIRING_LEADER]: JIUQU_ROLE_TEACHER_ASSIST,
    [ROLE_HIRING]: JIUQU_ROLE_TEACHER_ASSIST,
    [ROLE_STUDENT_SERVICE_LEADER]: JIUQU_ROLE_STUDENT_ASSIST,
    [ROLE_STUDENT_SERVICE]: JIUQU_ROLE_STUDENT_ASSIST,
    [ROLE_TEACHER_SERVICE_LEADER]: JIUQU_ROLE_TEACHER_ASSIST,
    [ROLE_TEACHER_SERVICE]: JIUQU_ROLE_TEACHER_ASSIST,
    [ROLE_IT_SUPPORT]: JIUQU_ROLE_ADMIN,
    [ROLE_FINANCE]: JIUQU_ROLE_FINANCE,
    [ROLE_MARKETING]: JIUQU_ROLE_STUDENT_ASSIST,
    [ROLE_OTHER]: JIUQU_ROLE_STUDENT_ASSIST,
  };

  return mapping[roleId] || JIUQU_ROLE_STUDENT_ASSIST;
}

function getStudentBaseInfo(userInfo) {
  return {
    jiuquId: `Student:${userInfo.id}`,
    userGroupName: USER_GROUP_STUDENT,
  };
}

function getTeacherBaseInfo(userInfo) {
  return {
    jiuquId: `Teacher:${userInfo.id}`,
    userGroupName: USER_GROUP_TEACHER,
  };
}

function getAdminBaseInfo(userInfo) {
  let userGroupName;

  switch (toLegacyRole(userInfo.roleId)) {
    case JIUQU_ROLE_ADMIN:
      userGroupName = USER_GROUP_ADMIN;
      break;
    case JIUQU_ROLE_STUDENT_ASSIST:
      userGroupName = USER_GROUP_STUDENT_ASSIST;
      break;
    case JIUQU_ROLE_TECH_SUPPORT:
      userGroupName = USER_GROUP_TECH_SUPPORT;
      break;
    case JIUQU_ROLE_TEACHER_ASSIST:
      userGroupName = USER_GROUP_TEACHER_ASSIST;
      break;
    case JIUQU_ROLE_TEACHER_TRAINER:
      userGroupName = USER_GROUP_TEACHER_TRAINER;
      break;
    case JIUQU_ROLE_FINANCE:
      userGroupName = USER_GROUP_FINANCE;
      break;
    default:
      userGroupName = USER_GROUP_ADMIN;
      break;
  }

  return {
    jiuquId: `Admin:${userInfo.id}`,
    userGroupName,
  };
}


function getUserBaseInfo(userInfo, role) {
  const roleHandlers = {
    student: getStudentBaseInfo,
    admin: getAdminBaseInfo,
    teacher: getTeacherBaseInfo,
  };
  const baseInfoDetector = roleHandlers[role] || getAdminBaseInfo;

  return baseInfoDetector(userInfo);
}

export { getUserBaseInfo };
